package main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class EliminarPartidasMenor4Jugadores {
	private static Connection conexion;
	private static ArrayList<Integer> listaPartidas;
	private static ArrayList<Integer> listaJugadores;
	private static ArrayList<Integer> listaTotalJugadores;
	private static ArrayList<Float> listaSkills;
	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		Class.forName("com.mysql.jdbc.Driver");
		conexion = DriverManager.getConnection("jdbc:mysql://localhost/proyecto", "root", "gracias");
		Statement st = conexion.createStatement();
		Statement st1 = conexion.createStatement();
		Statement st2 = conexion.createStatement();
		ResultSet rs; ResultSet rs1; ResultSet rs2;
		listaPartidas = new ArrayList<Integer>();
		listaJugadores = new ArrayList<Integer>();
		listaSkills = new ArrayList<Float>();
		listaTotalJugadores = new ArrayList<Integer>();
		int numJugadores; int partida = 0;
		rs2 = st2.executeQuery("SELECT idMatch FROM matchsummary");
		while(rs2.next()) {
			partida = rs2.getInt("idMatch");
			rs = st.executeQuery("SELECT COUNT(*) FROM flageventsummaryplayer WHERE team = 'blue' AND idMatch = '"+partida+"'");
			while(rs.next()) {
				numJugadores = rs.getInt(1);
				if(numJugadores < 2) {
					listaPartidas.add(partida);
				}
				else {
					rs1 = st1.executeQuery("SELECT COUNT(*) FROM flageventsummaryplayer WHERE team = 'red' AND idMatch = '"+partida+"'");
					while(rs1.next()) {
						numJugadores = rs1.getInt(1);
						if(numJugadores < 2) listaPartidas.add(partida);
					}
				}
			}
		}
		System.out.println("Numero de partidas a eliminar: "+listaPartidas.size());
		
		int size = listaPartidas.size();
		for(int i = 0; i < size; i++) {
			System.out.println("Eliminando partida: "+i);
			partida = listaPartidas.get(i);
			st.execute("DELETE FROM flageventsummaryplayer WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM flageventsummaryteam WHERE idMatch = '"+partida+"'");
			//actualizaGamePlayMatchUser(partida);
			eliminaPartidaGamePlayMatchUser(partida);
			st.execute("DELETE FROM gameplaymatchuser WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM gamesummarymatchplayer WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM killingspreesmatch WHERE idPartida = '"+partida+"'");
			st.execute("DELETE FROM matchsummary WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM playerspecifickillsdeaths WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM specialeventsplayer WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM suicidestotalsplayer WHERE idMatch = '"+partida+"'");
			st.execute("DELETE FROM weaponspecifictotalsplayer WHERE idMatch = '"+partida+"'");
		}
		actualizaGamePlayMatchUser();
	}
	
	private static void eliminaPartidaGamePlayMatchUser(int partida) throws ClassNotFoundException, SQLException {
		Statement st = conexion.createStatement();
		ResultSet rs; 
		int id; int num; float skill;
		rs = st.executeQuery("SELECT idPlayer, numberMatch, previousSkill FROM gameplaymatchuser WHERE idMatch = '"+partida+"'");
		while(rs.next()) {
			num = rs.getInt("numberMatch");
			id = rs.getInt("idPlayer");
			if(!listaTotalJugadores.contains(id)) listaTotalJugadores.add(id);
			if(num == 1) {
				skill = rs.getFloat("previousSkill");
				listaJugadores.add(id);
				listaSkills.add(skill);
			}
		}
		//st.execute("DELETE FROM gameplaymatchuser WHERE idMatch = '"+partida+"'");
	}
	
	private static void actualizaGamePlayMatchUser() throws ClassNotFoundException, SQLException {
		Statement st = conexion.createStatement();
		Statement st1 = conexion.createStatement();
		Statement st2 = conexion.createStatement();
		Statement st3 = conexion.createStatement();
		ResultSet rs; ResultSet rs3; ResultSet rs1;
		int id; int num = 0; float skill = 0; int maxPartida = 0; int numPartida; int cont = 1; int numJug = 1;
		//rs = st.executeQuery("SELECT idPlayer FROM players");
		//while(rs.next()) {
		int size = listaTotalJugadores.size();
		for(int i = 0; i < size; i++) {
			System.out.println("Actualizando jugador: "+i);
			cont = 1;
			//id = rs.getInt("idPlayer");
			id = listaTotalJugadores.get(i);
			rs3 = st3.executeQuery("SELECT max(numberMatch) FROM gameplaymatchuser WHERE idPlayer = '"+id+"'");
			while(rs3.next()) {
				maxPartida = rs3.getInt(1);
			}
			rs3 = st3.executeQuery("SELECT count(*) FROM gameplaymatchuser WHERE idPlayer = '"+id+"'");
			while(rs3.next()) {
				num = rs3.getInt(1);
			}
			if(num != maxPartida) {
				rs1 = st1.executeQuery("SELECT numberMatch FROM gameplaymatchuser WHERE idPlayer = '"+id+"' ORDER BY numberMatch ASC");
				while(rs1.next()) {
					numPartida = rs1.getInt("numberMatch");
					if(cont == 1) {
						if(listaJugadores.contains(id)) {
							skill = listaSkills.get(listaJugadores.indexOf(id));
						}
					}
					if(skill != 0) {
						st2.executeUpdate("UPDATE gameplaymatchuser SET numberMatch = '"+cont+"', previousSkill = '"+skill+"' WHERE numberMatch = '"+numPartida+"' AND idPlayer = '"+id+"'");
						skill = 0;
					}
					else {
						//st2.execute("DELETE FROM gameplaymatchuser WHERE idPlayer = '"+id+"' AND numberMatch = '"+numPartida+"'");
						st2.executeUpdate("UPDATE gameplaymatchuser SET numberMatch = '"+cont+"' WHERE numberMatch = '"+numPartida+"' AND idPlayer = '"+id+"'");
					}
					cont++;
				}
			}
			st2.executeUpdate("UPDATE gamesummaryglobalplayer SET numberMatches = '"+num+"' WHERE idPlayer = '"+id+"'");
			numJug++;
		}
		/*rs = st.executeQuery("SELECT idPlayer, numberMatch, previousSkill FROM gameplaymatchuser WHERE idMatch = '"+partida+"'");
		st1.execute("DELETE FROM gameplaymatchuser WHERE idMatch = '"+partida+"'");
		while(rs.next()) {
			id = rs.getInt("idPlayer");
			num = rs.getInt("numberMatch");
			skill = rs.getFloat("previousSkill");
			
			rs3 = st3.executeQuery("SELECT max(numberMatch) FROM gameplaymatchuser WHERE idPlayer = '"+id+"'");
			while(rs3.next()) {
				maxPartida = rs3.getInt(1);
			}
			for(int i = num; i < maxPartida; i++) {
				nuevoNum = i + 1;
				if(skill != 0) {
					st1.executeUpdate("UPDATE gameplaymatchuser SET numberMatch = '"+i+"', previousSkill = '"+skill+"' WHERE numberMatch = '"+nuevoNum+"' AND idPlayer = '"+id+"'");
					skill = 0;
				}
				else st1.executeUpdate("UPDATE gameplaymatchuser SET numberMatch = '"+i+"' WHERE numberMatch = '"+nuevoNum+"' AND idPlayer = '"+id+"'");
			}
			st1.executeUpdate("UPDATE gamesummaryglobalplayer SET numberMatches = (numberMatches - 1) WHERE idPlayer = '"+id+"'");
		}
		rs.close();*/
	}

}
